<template>
  <el-table :data="contentArr" :teleported="false">
    <el-table-column type="index" label="编号" width="60px"></el-table-column>
    <el-table-column prop="title" label="标题"></el-table-column>
    <el-table-column label="封面" width="70px">
      <template #default="scope">
       <img :src="BASE_URL+scope.row.imgUrl" style="width: 60px">
      </template>
    </el-table-column>
    <el-table-column prop="brief" label="摘要" width="400px"></el-table-column>
    <el-table-column prop="categoryName" label="分类" width="70px"></el-table-column>
    <el-table-column prop="viewCount" label="浏览量" width="70px"></el-table-column>
    <el-table-column prop="commentCount" label="评论量" width="70px"></el-table-column>
    <el-table-column prop="createTime" label="创建时间"></el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="mini" type="success" @click="editContent(scope.row)">编辑</el-button>
        <el-button size="mini" type="danger" @click="deleteContent(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import {onMounted, ref} from "vue";
import router from "@/router";
import axios from "axios";
import {ElMessage} from 'element-plus'


const contentArr=ref([])
//获取全部内容信息,并将全部的内容存入contentArr中
onMounted(()=>{
  axios.get(BASE_URL+'/v1/contents/list/admin')
      .then((response)=>{
        if (response.data.code==1){
          contentArr.value=response.data.data;
        }
      })
})

const deleteContent=(i,c)=>{
  if (confirm("您确认删除吗?")){
    //根据内容的id进行删除
    axios.post(BASE_URL+'/v1/contents/'+c.id+'/delete')
      .then((response)=>{
        if (response.data.code==1){
          ElMessage.success("删除成功!");
          contentArr.value.splice(i,1)
        }
      })
  }
}
//修改功能
const editContent=(c)=>{
  router.push('/personal/post?id='+c.id);
  localStorage.editFrom='admin';//设置修改来源页面
}
</script>

<style scoped>

</style>